package com.foreknow.elm.controller;

import com.foreknow.elm.po.User;
import com.foreknow.elm.service.UserService;
import com.foreknow.elm.service.impl.UserServiceImpl;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class UserController {
    public Object saveUser(HttpServletRequest request, HttpServletResponse response) throws Exception{
        String userId = request.getParameter("userId");
        String password = request.getParameter("password");
        String userName = request.getParameter("userName");
        Integer userSex = Integer.valueOf(request.getParameter("userSex"));
        User user = new User(userId,password,userName,userSex,null);
        UserService service = new UserServiceImpl();
        int result = service.saveUser(user);
        return result;
    }
    public Object getUserByIdByPass(HttpServletRequest request,HttpServletResponse response) throws Exception{
        String userId = request.getParameter("userId");
        String password = request.getParameter("password");
        UserService service = new UserServiceImpl();
        User user = service.getUserByIdByPass(userId, password);
        if(user!=null) {
            request.getSession().setAttribute("user", user);
        }
        return user;
    }
    public Object getUserById(HttpServletRequest request,HttpServletResponse response) throws Exception{
        String userId = request.getParameter("userId");
        UserService service = new UserServiceImpl();
        User user = service.getUserById(userId);
        return user;
    }
}
